<?php
class CartModel extends Eloquent {
    protected $table = 'cart';
    protected $primaryKey  = 'cart_id';
    static public function getCartList($user_id,$per_page=10,$page = null){
        $res = new self();
        $res->where('user_id',$user_id)
            ->paginate($per_page);
        if($res){
            $page = $res->toArray();
            $data = $page['data'];
        } else {
            $data = array();
        }
        return $data;
    }
    static public function setCart($user_id,$product_id,$quantity,$product_discount_id = 0){
        $res = self::where('user_id',$user_id)
            ->where('product_id',$product_id)
            ->where('product_discount_id',$product_discount_id)
            ->first();
        if($res){
            return true;
        } else {
            $res = new self();
            $res->user_id = $user_id;
            $res->product_id = $product_id;
            $res->quantity = $quantity;
            $res->product_discount_id = $product_discount_id;
            if($res->save()){
                return true;
            }else{
                return false;
            }
        }
    }
}


